package com.softaria.gwt.common.client.dialog;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant;
import com.softaria.gwt.common.client.widget.Button;

public class Dialog extends DialogBox {

  private VerticalPanel generalPanel;
  private HorizontalPanel buttonPanel;
  private SimplePanel opacityPanel;
  private Widget content;

  public Dialog(String title, String message) {
    this(title, new HTML(message));
  }

  public Dialog(String title, Widget content) {
    super(false, true);
    this.content = content;

    setStylePrimaryName("sa-dialog");
    setText(title);

    // Left top corner
    String src = GWT.getModuleName() + "/common/images/dialog/corner_left_ie.png";
    final Element leftTopCorner = DOM.getParent(getCellElement(0, 0));
    DOM.setStyleAttribute(leftTopCorner, "filter",
        "progid:DXimageTransform.Microsoft.AlphaimageLoader(src='" + src
            + "', sizingMethod='crop')");

    // Left top corner
    src = GWT.getModuleName() + "/common/images/dialog/corner_right_ie.png";
    final Element rightTopCorner = DOM.getParent(getCellElement(0, 2));
    DOM.setStyleAttribute(rightTopCorner, "filter",
        "progid:DXimageTransform.Microsoft.AlphaimageLoader(src='" + src
            + "', sizingMethod='crop')");

    // general panel
    generalPanel = new VerticalPanel();
    generalPanel.setStylePrimaryName("container");

    generalPanel.add(content);
    content.setStylePrimaryName("content");
    generalPanel.setCellHorizontalAlignment(content, HasHorizontalAlignment.ALIGN_CENTER);
    generalPanel.setCellVerticalAlignment(content, HasVerticalAlignment.ALIGN_MIDDLE);
    add(generalPanel);

    // button panel
    buttonPanel = new HorizontalPanel();
    buttonPanel.setStylePrimaryName("buttons");
    buttonPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
    generalPanel.add(buttonPanel);
  }

  public void setTextAlignment(HorizontalAlignmentConstant alignment) {
    generalPanel.setCellHorizontalAlignment(content, alignment);
  }

  @Override
  public void show() {
    // show dark-screen
    opacityPanel = new SimplePanel();
    opacityPanel.setStylePrimaryName("sa-opacityPanel");
    RootPanel.get().add(opacityPanel);

    super.show();
  }

  @Override
  public void hide() {
    super.hide();

    // hide dark-screen
    RootPanel.get().remove(opacityPanel);
  }

  public void addButton(Button button) {
    buttonPanel.add(button);
  }

  public void setMessage(String text) {
    if (content instanceof HTML) {
      HTML html = (HTML) content;
      html.setHTML(text);
    }
  }
}
